package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.EnterpriseEmployee
import cn.exrick.xboot.base.entity.JobEnterprise
import cn.exrick.xboot.base.entity.SafeScoreRecord
import cn.exrick.xboot.base.mapper.SafeScoreRecordMapper
import cn.exrick.xboot.base.service.IEnterpriseEmployeeService
import cn.exrick.xboot.base.service.IEnterpriseUserService
import cn.exrick.xboot.base.service.ISafeScoreRecordService
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.math.BigDecimal

/**
 * 安全得分记录接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class ISafeScoreRecordServiceImpl : ServiceImpl<SafeScoreRecordMapper?, SafeScoreRecord?>(), ISafeScoreRecordService {


    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var iEnterpriseUserService: IEnterpriseUserService

    @Autowired
    private lateinit var iEnterpriseEmployeeService: IEnterpriseEmployeeService

    override fun createOrUpdateScoreRecord(safeScoreRecord: SafeScoreRecord): Result<Any?>? {

        return try {

            safeScoreRecord.enterpriseId = iEnterpriseEmployeeService.getById(safeScoreRecord.employeeId)?.enterpriseId
            saveOrUpdate(safeScoreRecord)

            iEnterpriseEmployeeService.getById(safeScoreRecord.employeeId)?.run {

                safeScore = when (safeScoreRecord.addOrSub) {
                    0 -> {
                        BigDecimal(safeScore.toString()).add(BigDecimal(safeScoreRecord.score.toString())).intValueExact()
                    }
                    else -> {
                        BigDecimal(safeScore.toString()).subtract(BigDecimal(safeScoreRecord.score.toString())).intValueExact()
                    }
                }
                iEnterpriseEmployeeService.updateById(this)
            }
            ResultUtil.success("操作成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun removeScoreRecord(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success("移除成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun getJScoreRecordList(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        return list(KtQueryWrapper(SafeScoreRecord::class.java).eq(SafeScoreRecord::employeeId, iEnterpriseEmployeeService.getOne(KtQueryWrapper(EnterpriseEmployee::class.java)
                .eq(EnterpriseEmployee::userId, securityUtil.currUser.id))?.id )
                .orderByDesc(JobEnterprise::createTime)).run {

            val result:MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, this)
            result["score"] = iEnterpriseEmployeeService.getOne(KtQueryWrapper(EnterpriseEmployee::class.java)
                    .eq(EnterpriseEmployee::userId, securityUtil.currUser.id))?.safeScore

            ResultUtil.data(result, "获取数据成功！")
        }
    }

    override fun getDetailById(id: String?): Result<SafeScoreRecord?>? {

        return ResultUtil.data(this.getById(id))
    }


}